package com.whu.waterworks.service;

import java.util.ArrayList;
import java.util.List;

import com.whu.waterworks.dao.AlertDAO;
import com.whu.waterworks.model.Alert;

public class AlertService {
	AlertDAO alertDAO;
	public List<Alert> getAll() {
		return alertDAO.findAll();
	}
	
	//获取所有的预警类型
	public List<Alert> findAlertTypes() {
		List<Alert> alerts =getAll();
		List<String> alertTypes=new ArrayList<>();
		if (alerts==null) {
			return null;
		}
		else {
			for (Alert alert : alerts) {
				alertTypes.add(alert.getAlertType());
			}
		}
		return null;
	}
	
	//获取预警类型
	public Alert findByAlertType(String alertType) {
		List<Alert> alerts =getAll();
		for (Alert alert : alerts) {
			if (alert.getAlertType().equals(alertType)) {
				return alert;
			}
		}
		return null;
	}
	 
	public Boolean saveAlert(Alert alert) {
		try {
			alertDAO.save(alert);
			return true;
		} catch (Exception e) {
			
			System.out.println(e.toString());
			return false;
		}
	}
	
	public Boolean updateAlert(Alert alert) {
		try {
			alertDAO.update(alert);
			return true;
		} catch (Exception e) {
			
			System.out.println(e.toString());
			return false;
		}
	}
	
	public AlertDAO getAlertDAO() {
		return alertDAO;
	}

	public void setAlertDAO(AlertDAO alertDAO) {
		this.alertDAO = alertDAO;
	}
	

}
